package labox.innovation.util.crypt;

import labox.innovation.util.Rnd;

/**
 * Blowfish keygen for LoginServer client connections
 * 
 * @author KenM
 */
public class BlowFishKeygenForLogin {
	
	private static final int CRYPT_KEYS_SIZE = 20;

	private static final byte[][] CRYPT_KEYS = new byte[CRYPT_KEYS_SIZE][16];

	static {
		// init the GS encryption keys on class load

		for (int i = 0; i < CRYPT_KEYS_SIZE; i++) {
			// randomize the 8 first bytes
			for (int j = 0; j < CRYPT_KEYS[i].length; j++) {
				CRYPT_KEYS[i][j] = (byte) Rnd.get(255);
			}

			// the last 8 bytes are static
			CRYPT_KEYS[i][8] = (byte) 0x56;
			CRYPT_KEYS[i][9] = (byte) 0xb0;
			CRYPT_KEYS[i][10] = (byte) 0xf3;
			CRYPT_KEYS[i][11] = (byte) 0x02;
			CRYPT_KEYS[i][12] = (byte) 0x9e;
			CRYPT_KEYS[i][13] = (byte) 0x88;
			CRYPT_KEYS[i][14] = (byte) 0x2d;
			CRYPT_KEYS[i][15] = (byte) 0xee;
		}
	}

	// block instantiation
	private BlowFishKeygenForLogin() {

	}

	/**
	 * Returns a key from this keygen pool, the logical ownership is retained by this keygen.<BR>
	 * Thus when getting a key with interests other then read-only a copy must be performed.<BR>
	 * 
	 * @return A key from this keygen pool.
	 */
	public static byte[] getRandomKey() {
		return CRYPT_KEYS[Rnd.get(CRYPT_KEYS_SIZE)];
	}
}
